using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Beta_fundies_controls_comments_SubmitComment : Controls.DataManagerQuoteControl
{
    public event EventHandler CommentCreated;
    protected override void ItemChanged()
    {
        if (CurrentQuote != null)
        {
            if (CurrentQuote.CommentState == DAL.Quote.CommentStatus.Open)
            {
                mviewSubmit.SetActiveView(viewSubmit);
            }
            else
            {
                mviewSubmit.SetActiveView(viewClosed);
            }
        }
        else
        {
            mviewSubmit.SetActiveView(viewError);
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid && chkTuring.Checked == true)
        {
            DAL.Comment myComment = new DAL.Comment();
            myComment.Author = txtUsername.Text;
            myComment.Post = txtComment.Text;
            myComment.IPAddress = HttpContext.Current.Request.UserHostAddress;
            myComment.Quote = CurrentQuote;
            DAL.Comments.Add(myComment);

            CookiedUsername = txtUsername.Text;

            ClearFields();

            if (CommentCreated != null)
            {
                CommentCreated(this, new EventArgs());
            }
        }
    }

    protected void ClearFields()
    {
        txtUsername.Text = CookiedUsername;
        txtComment.Text = "";
    }

    private string CookiedUsername
    {
        get
        {
            return DAL.CookieManager.TryGetCookie("commentusername", "");
        }
        set
        {
            DAL.CookieManager.SetCookie("commentusername", value, DateTime.Now.AddYears(1));
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ClearFields();
        }
    }
}
